Learn R Programming

dendextend (version 0.14.2)

labels<-: "label" assignment operator

Description

"label" assignment operator for vectors, dendrogram, and hclust classes.

Usage

labels(object, ...) <- value

## S3 method for class 'matrix':
labels(object, which = c("colnames", "rownames"), ...)

## S3 method for class 'matrix':
labels(object, which = c("colnames", "rownames"), ...) <- value

## S3 method for class 'dendrogram':
labels(object, ...) <- value

## S3 method for class 'hclust':
labels(object, order = TRUE,...)

## S3 method for class 'hclust':
labels(object, ...) <- value

Arguments

object
a variable name (possibly quoted) who's label are to be updated
which
"colnames" or "rownames", to which of the two should labels refer to.
...
parameters passed (not currently in use)
value
a value to be assigned to object's label
order
default is FALSE. Only relevant for extracting labels from an hclust object (with labels.hclust). Setting order=TRUE will return labels in their order in the dendrogram, instead of the riginal labels order re

Value

  • The updated object

source

The functions here are based on code by Gavin and kohske from (adopted to dendrogram by Tal Galili): http://stackoverflow.com/questions/4614223/how-to-have-the-following-work-labelsx-some-value-r-question Also with some ideas from Gregory Jefferis's dendroextras package.

See Also

labels, labels.matrix

Examples

Run this code
x <- 1:3
labels(x)
labels(x) <- letters[1:3]
labels(x) # [1] "a" "b" "c"
x
# a b c
# 1 2 3


# get("labels<-")

################
# Example for matrix objects

x <- matrix(1:9, 3,3)
labels(x)
x
labels(x) <- letters[1:3]
x
labels(x, which = "rownames") <- LETTERS[24:26]
x
#  a b c
#X 1 4 7
#Y 2 5 8
#Z 3 6 9

################
# Example for using the assignment with dendrogram and hclust objects:
hc <- hclust(dist(USArrests[1:3,]), "ave")
dend <- as.dendrogram(hc)

labels(hc) # "Arizona" "Alabama" "Alaska"
labels(hc)  <- letters[1:3]
labels(hc)# "a" "b" "c"
labels(dend) # "Arizona" "Alabama" "Alaska"
labels(dend) <- letters[1:3]
labels(dend) # "a" "b" "c"
labels(dend) <- LETTERS[1:2] # will produce a warning
labels(dend) # "A" "B" "A"
labels(dend) <- LETTERS[4:6] # will replace the labels correctly
# (the fact the tree had duplicate labels will not cause a problem)
labels(dend) # "D" "E" "F"

Run the code above in your browser using DataLab